package com.zou.music_downloader.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zou.music_downloader.R;
import com.zou.music_downloader.bean.DataBean;
import com.zou.music_downloader.sqlite.MyDao;
import com.zou.music_downloader.utils.TimeFormatter;

import java.io.File;
import java.util.List;

/**
 * Created by zou on 2016/3/16.
 */
public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.MyViewHolder> {

    List<DataBean> datas;
    Context        context;
    MyDao          myDao;

    public HistoryAdapter(Context context, List<DataBean> datas) {
        this.context = context;
        this.datas = datas;
        myDao = new MyDao(context);

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);

        return new MyViewHolder(layoutInflater.inflate(R.layout.item_history, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder.title.setText(datas.get(position).getSongName() + " - " + datas.get(position).getAuthorName());
        holder.time.setText(TimeFormatter.cast(datas.get(position).getDownloadTime()));
        holder.state.setText(datas.get(position).getState());



        if (datas.get(position).getState().equals("已下载")) {
            holder.item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent("android.intent.action.VIEW");
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent.putExtra("oneshot", 0);
                    intent.putExtra("configchange", 0);
                    Uri uri = Uri.fromFile(new File(datas.get(position).getPath()));
                    intent.setDataAndType(uri, "audio/*");
                    context.startActivity(intent);
                }
            });
        }
/*//        防止未收到广播而显示错误
        if (datas.get(position).getState().equals("已开始") && new File(datas.get(position).getPath()).exists()) {
            myDao.updateData(datas.get(position).getDownloadId(), "已下载");
            holder.state.setText("已下载");
        }*/


    }

    @Override
    public int getItemCount() {
        if (datas == null) {
            return 0;
        }
        return datas.size();
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
        View     item;
        TextView title;
        TextView time;
        TextView state;

        public MyViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.tv_downloaded_title);
            time = (TextView) itemView.findViewById(R.id.tv_downloaded_time);
            state = (TextView) itemView.findViewById(R.id.tv_downloaded_state);
            item = itemView;
        }
    }


}
